<?php
include "config.php";
/*
 tid       | integer                | not null default nextval('trace_tid_seq'::regclass)
 uid       | bigint                 | not null
 tracetype | integer                | not null default 1
 hidecnt   | integer                | not null default 0
 title     | character varying(255) |

*/
$objDB = new pgsql();
$SQL = "SELECT  nextval('trace_tid_seq')";
$objDB->query($SQL);
$tidArr = $objDB->fetch();

$tid = $tidArr[0]['nextval'];

$SQL = "insert into trace (tid, uid, tracetype, hidecnt, title, rdate) values($tid, '".$_REQUEST['uid'] . "', 0, 0, '".$_REQUEST['title']."', now())";
$res = $objDB->query($SQL);

$tmpCnt = count($_FILES['uploadedfile']['name']);
$isOK = true;
for($i=0;$i<$tmpCnt;$i++) {
	if($_FILES['uploadedfile']['name'][$i]) {
		$target_path = "/data01/files/";
		$thumb_path = $target_path . "thumb/";
		$tmp_img = explode("." ,$_FILES['uploadedfile']['name'][$i]); 
		$img_name = $i . "_" . $uid . "_" . $tid."_".time().".".$tmp_img[count($tmp_img) - 1];

		$target_path = $target_path . basename($img_name);
		$thumb_path = $thumb_path . basename($img_name);
		if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'][$i], $target_path)) {
			if(move_uploaded_file($_FILES['thumbfile']['tmp_name'][$i], $thumb_path)) {
				$SQL = "insert into tracefile (tid, path, ftype) values(" . $tid . ", '".$img_name."', 1)"; ///<ftype 1 = image

				if($objDB->query($SQL)) {
					$isOK = true;
				} else {
					$isOK = false;
				}
			} else {
				$isOK = false;
			}
		} else {
			$isOK = false;
		}
	}
}

if($isOK) {
	echo "{\"SUCCESS\":\"1\",\"MESSAGE\":\"success\"}";
	exit;
} else {
	echo "{\"SUCCESS\":\"0\",\"MESSAGE\":\"업로드에 문제가 발생했습니다. 잠시후 다시 이용해 주시기 바랍니다.\"}";
	exit;
}
exit;
/*
if($_FILES['uploadedfile']['name']) {
	$target_path = "/data01/files/";
	$thumb_path = $target_path . "thumb/";
	$tmp_img = explode("." ,$_FILES['uploadedfile']['name']); 
	$img_name = $uid . "_" . $tid."_".time().".".$tmp_img[count($tmp_img) - 1];
	$target_path = $target_path . basename($img_name);
	$thumb_path = $thumb_path . basename($img_name);
	if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
		if(move_uploaded_file($_FILES['thumbfile']['tmp_name'], $thumb_path)) {
			$SQL = "insert into tracefile (tid, path, ftype) values(" . $tid . ", '".$img_name."', 1)"; ///<ftype 1 = image

			if($objDB->query($SQL)) {
				echo "{\"SUCCESS\":\"1\",\"MESSAGE\":\"success\"}";
				exit;
			} else {
				echo "{\"SUCCESS\":\"0\",\"MESSAGE\":\"업로드에 문제가 발생했습니다. 잠시후 다시 이용해 주시기 바랍니다.\"}";
				exit;
			}
		}
	} else {
		echo "{\"SUCCESS\":\"0\",\"MESSAGE\":\"업로드에 문제가 발생했습니다. 잠시후 다시 이용해 주시기 바랍니다.\"}";
		exit;
	}
} else {
	if($res) {
		echo "{\"SUCCESS\":\"1\",\"MESSAGE\":\"success\"}";
		exit;
	} else {
		echo "{\"SUCCESS\":\"0\",\"MESSAGE\":\"업로드에 문제가 발생했습니다. 잠시후 다시 이용해 주시기 바랍니다.\"}";
		exit;
	}
}/**/
/*/
function make_thumbnail($source_path, $width, $height, $thumbnail_path){
    list($img_width,$img_height, $type) = getimagesize($source_path);
    if ($type!=1 && $type!=2 && $type!=3 && $type!=15) return;
    if ($type==1) $img_sour = imagecreatefromgif($source_path);
    else if ($type==2 ) $img_sour = imagecreatefromjpeg($source_path);
    else if ($type==3 ) $img_sour = imagecreatefrompng($source_path);
    else if ($type==15) $img_sour = imagecreatefromwbmp($source_path);
    if ($img_width > $img_height) {
        $w = round($height*$img_width/$img_height);
        $h = $height;
        $x_last = round(($w-$width)/2);
        $y_last = 0;
    } else {
        $w = $width;
        $h = round($width*$img_height/$img_width);
        $x_last = 0;
        $y_last = round(($h-$height)/2);
    }
    if ($img_width < $width && $img_height < $height) {
        $img_last = imagecreatetruecolor($width, $height);
        $x_last = round(($width - $img_width)/2);
        $y_last = round(($height - $img_height)/2);

        imagecopy($img_last,$img_sour,$x_last,$y_last,0,0,$w,$h);
        imagedestroy($img_sour);
        $white = imagecolorallocate($img_last,255,255,255);
        imagefill($img_last, 0, 0, $white);
    } else {
        $img_dest = imagecreatetruecolor($w,$h);
        imagecopyresampled($img_dest, $img_sour,0,0,0,0,$w,$h,$img_width,$img_height);
        $img_last = imagecreatetruecolor($width,$height);
        imagecopy($img_last,$img_dest,0,0,$x_last,$y_last,$w,$h);
        imagedestroy($img_dest);
    }
    if ($thumbnail_path) {
        if ($type==1) imagegif($img_last, $thumbnail_path, 100);
        else if ($type==2 ) imagejpeg($img_last, $thumbnail_path, 100);
        else if ($type==3 ) imagepng($img_last, $thumbnail_path, 100);
        else if ($type==15) imagebmp($img_last, $thumbnail_path, 100);
    } else {
        if ($type==1) imagegif($img_last);
        else if ($type==2 ) imagejpeg($img_last);
        else if ($type==3 ) imagepng($img_last);
        else if ($type==15) imagebmp($img_last);
    }
    imagedestroy($img_last);
}
/**/
?>
